package com.zhijian.medical.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.zhijian.medical.dao.local.RoutinePatientRegistMapper;
import com.zhijian.medical.entity.po.RoutinePatientRegist;
import com.zhijian.medical.entity.vo.request.RoutinePatientRegistSearchVo;
import com.zhijian.medical.entity.vo.request.RoutinePatientRegistSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.RoutinePatientRegistVo;
import com.zhijian.medical.service.RoutinePatientRegistService;
import com.zhijian.medical.service.RoutinePatientService;
import com.zhijian.medical.util.RandomStringGenerator;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 常规病理-病人-登记信息业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-11-05 13:21:19
 */
@Slf4j
@Service
public class RoutinePatientRegistServiceImpl implements RoutinePatientRegistService {

	@Autowired
    private RoutinePatientRegistMapper mapper;
    @Autowired
    private RoutinePatientService patientService;

    @Override
    public RoutinePatientRegist selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public RoutinePatientRegistVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(RoutinePatientRegist routinePatientRegist) {
        Date now = new Date();
        routinePatientRegist.setCreateTime(now);
        routinePatientRegist.setUpdateTime(now);
        return mapper.insert(routinePatientRegist) > 0 ? routinePatientRegist.getId() : 0L;
    }

    @Override
    public Long insertSelective(RoutinePatientRegist routinePatientRegist) {
        Date now = new Date();
        routinePatientRegist.setCreateTime(now);
        routinePatientRegist.setUpdateTime(now);
        return mapper.insertSelective(routinePatientRegist) > 0 ? routinePatientRegist.getId() : 0L;
    }

    @Override
    public boolean update(RoutinePatientRegist routinePatientRegist) {
        Date now = new Date();
        routinePatientRegist.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(routinePatientRegist) != 0;
    }

    @Override
    public boolean updateByBlhAndType(RoutinePatientRegist record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByBlhAndType(record) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(RoutinePatientRegist record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(RoutinePatientRegistSaveOrUpdateVo saveOrUpdateVo) {
        RoutinePatientRegist routinePatientRegist = new RoutinePatientRegist();
        BeanUtils.copyProperties(saveOrUpdateVo, routinePatientRegist);
        return this.insert(routinePatientRegist) != 0;
    }

    @Override
    public boolean modify(RoutinePatientRegistSaveOrUpdateVo saveOrUpdateVo) {
        RoutinePatientRegist old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        RoutinePatientRegist routinePatientRegist = new RoutinePatientRegist();
        BeanUtils.copyProperties(saveOrUpdateVo, routinePatientRegist);
        return this.update(routinePatientRegist);
    }

    @Override
    public RoutinePatientRegistVo selectOne(RoutinePatientRegistSearchVo routinePatientRegistSearchVo) {
        return mapper.selectOne(routinePatientRegistSearchVo);
     }

    @Override
    public Integer count(RoutinePatientRegistSearchVo routinePatientRegistSearchVo) {
        return mapper.count(routinePatientRegistSearchVo);
    }

    @Override
    public List<RoutinePatientRegistVo> selectList(RoutinePatientRegistSearchVo routinePatientRegistSearchVo) {
        return mapper.selectList(routinePatientRegistSearchVo);
    }

    @Override
    public Page<RoutinePatientRegistVo> selectPage(RoutinePatientRegistSearchVo routinePatientRegistSearchVo) {
        AssertUtil.check(Objects.isNull(routinePatientRegistSearchVo.getPageNum()) || Objects.isNull(routinePatientRegistSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(routinePatientRegistSearchVo);
        //分页
        Page<RoutinePatientRegistVo> page = new Page<>(true, count, routinePatientRegistSearchVo.getPageNum(), routinePatientRegistSearchVo.getPageSize());
        routinePatientRegistSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<RoutinePatientRegistVo> responseVos = this.selectList(routinePatientRegistSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<RoutinePatientRegist> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(RoutinePatientRegistSearchVo routinePatientRegistSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(routinePatientRegistSearchVo);
    }

    @Override
    public RoutinePatientRegistVo selectByHisId(String hisNum, Integer type) {
        String blh = patientService.createNextMoleculePathologyNumber();
        RandomStringGenerator strGenerator = new RandomStringGenerator();
        String dateString = strGenerator.generateRandomDateString(1900, 2023);
        RoutinePatientRegistVo registVo = RoutinePatientRegistVo.builder()
                .blh(blh)
                .zyh(strGenerator.generate(6))
                .jzId(strGenerator.generate(6))
                .mzh(strGenerator.generate(6))
                .name(strGenerator.generate(4))
                .age(RandomUtil.randomInt(18, 40))
                .bingQu(strGenerator.generate(3))
                .chuangHao(strGenerator.generate(3))
                .gender(RandomUtil.randomInt(0, 1) + "")
                .minZu("汉族")
                .idCard(strGenerator.generateRandomIDCardNumber())
                .birth(dateString)
                .phone(strGenerator.generateRandomPhoneNumber())
                .address(strGenerator.generate(6))
                .marry(RandomUtil.randomInt(0, 1) == 0 ? "未婚" : "已婚")
                .jueJing(RandomUtil.randomInt(0, 1) == 0 ? "否" : "是")
                .type(RandomUtil.randomInt(0, 1) == 0 ? "住院" : "门诊")
                .diagnosis(strGenerator.generate(6))
                .yiZhu(strGenerator.generate(6))
                .amount(RandomUtil.randomInt(100, 1000) + "")
                .songJianDanWei(strGenerator.generate(6))
                .songJianKeShi(strGenerator.generate(6))
                .songJianDoctor(strGenerator.generate(3))
                .receiveTime(strGenerator.generateRandomDateString(2022, 2023))
                .build();
        if (type.equals(1)) {
            registVo.setZyh(hisNum);
        } else if (type.equals(3)) {
            registVo.setMzh(hisNum);
        }
        return registVo;
    }
}